
$(function () {
    $('th').addClass("text-center");
})
function sexfmt(value) {
    return value==1?"男":"女";
}
function fmtRole(value) {
    return value.roleName;
}
function dataFormatter() {
    var del = `<button class="del btn btn-danger"><i class="glyphicon glyphicon-trash"></i>删除</button>  `;
    var edit = `<button class="edit btn btn-warning"><i class="glyphicon glyphicon-pencil"></i>修改</button>`;
    return del+edit;
}
function date(value) {
    return value.substring(0,10);
}
//查询button的点击事件
function select() {
    var form = $('#form').serialize();
    var query = Qs.parse(form);
    $('#userTb').bootstrapTable("refresh",{query})
}
function queryParams(params){
    var formParams=$("#form").serialize();
    var query=Qs.parse(formParams);
    Object.assign(params,query);
    // console.log(params);
    return params;
}

$.get("user/select")
    .done(list=>{
        list.forEach(function (role) {
            var opt = $('<option/>');
            opt.val(role.id);
            opt.text(role.roleName);
            opt.appendTo('#form [name=roleId]');
        })
    });
function detailFormatter(index,row) {
    let html = `<table class="table table-bordered">
                   <tr>
                       <td>联系人姓名</td>
                       <td>联系人电话</td>
                       <td>联系人地址</td>
                   </tr>
                      `
    $(row.addressList).each(function () {
        html += `   <tr>
                       <td>${this.contact}</td>
                       <td>${this.tel}</td>
                       <td>${this.addressDesc}</td>
                    </tr>`

    });
    html += `</table>`;
    return html;
}
$('#grant').dialog({
         title:"用户授权",
         btnBind(){
             var arr1 = $('#userTb').bootstrapTable("getSelections");
             var arr2 = $('#grant table').bootstrapTable("getSelections");
             var params = `id=${arr1[0].id}&userRole=${arr2[0].id}`;
             // console.log(params);
             $.post('user/update',params)
                 .done(msg=>{
                     if(msg > 0){
                         swal({
                             type: "success",
                             title:"提示信息",
                             text:"授权成功！",
                             confirmButtonText: "确定"
                         });
                     }else {
                         swal({
                             type: "warning",
                             title:"提示信息",
                             text:"授权失败！",
                             confirmButtonText: "确定"
                         });
                     }
                     $('#userTb').bootstrapTable("refresh");
                     $('#grant table').dialog("hide");
                 });
         }
});
function a() {
    var arr = $('#userTb').bootstrapTable("getSelections");
    if(arr.length != 1){
        swal({
            type:"warning",
            title: "提示信息",
            text:"请选择一项"
        });
    }
    $('#grant table').bootstrapTable("checkBy",{field:"id",values:[arr[0].userRole]});
    $('#grant').dialog('show');
}
function ditu() {
    location.href = "user/diTu";
}
function Export() {
    location.href = "user/export";
}
